<template>
  <div>
    <!-- login部分 -->
    <div style="width:100%;height:250px;position:relative;border-bottom:1px solid #f7f7f7;">
      <div style="width:100%;text-align:center;position:absolute;top:40px;">
        <img src="/static/icon/bannilogo.png" style="width:120px;height:120px;border-radius:50%;" />
      </div>
      <div style="width:100%;text-align:center;position:absolute;top:190px;">
        <text style="font-size:24px;">伴你</text>
      </div>
    </div>
    <!-- 授权 -->
    <div>
      <div style="margin-left:30px;margin-top:20px;">
        <div style="100%">
          <text style="color:#888">登陆后该应用将会获取以下权限</text>
        </div>
        <div>
          <text style="color:#888">-获得你的公开信息（昵称、头像等）</text>
        </div>
      </div>
      <div style="width:100%;position:absolute;bottom:80px;">
        <div style="text-align:center;margin-left:20%;margin-right:20%">
          <button
            open-type="getUserInfo"
            @getuserinfo="getuserinfo"
            style="width: 400rpx;line-height: 80rpx;color: #ffffff;text-align: center;border-radius: 100rpx;background: linear-gradient(to right, #f83c57, #ffae6d);margin: 50rpx auto;"
          >微信授权</button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { user_commonRegister, wxLogin } from "../../../utils/wxUtil";
import { showToast } from "../../../utils/api-common";

export default {
  data() {
    return {
      code: "",
      phoneNum: "" //手机号
    };
  },
  methods: {
    //授权
    async getuserinfo(e) {
      if (this.phoneNum == "") {
        showToast("手机号为空");
        return;
      }

      // console.log("获取用户信息" + JSON.stringify(e));
      var errMsg = e.mp.detail.errMsg;
      if (errMsg == "getUserInfo:fail auth deny") {
        console.log("登录失败");
      } else {
        let encryptedData = e.mp.detail.encryptedData;
        let iv = e.mp.detail.iv;
        let userInfo = await user_commonRegister(
          encryptedData,
          iv,
          this.code,
          this.phoneNum
        );
        console.log("通用注册" + JSON.stringify(userInfo));
        if (userInfo) {
          this.$router.push({
            path: "/pages/mainPackage/chat/main",
            reLaunch: true
          });
        }
      }
    }
  },

  async beforeMount() {
    this.code = await wxLogin();
    this.phoneNum = this.$root.$mp.query.phoneNum;
  }
};
</script>
<style scoped>
</style>